package com.wc.AlgoOJ_train.train3_算法大礼包.c3_贪心.AG0402_修理熊舍;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.PriorityQueue;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2024/1/28 20:29
 * @description http://43.138.190.70:8888/p/AG0402
 */
public class Main {
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 20010;
    static Integer[] a = new Integer[N];
    static int n;
    static int x, y;

    /**
     * 哪两个便宜，哪两个来，都是俩俩合并 肯定是最小的两个合并价格最小
     */
    public static void main(String[] args) {
        n = sc.nextInt();
        PriorityQueue<Integer> q = new PriorityQueue<>();
        while (n-- > 0) {
            q.add(sc.nextInt());
        }
        long res = 0;
        while (q.size() >= 2) {
            x = q.poll();
            y = q.poll();
            res += y + x;
            q.add(y + x);
        }
        out.println(res);
        out.flush();
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}